
--------------------- 宝石类
Gem = {key=0,tpe=0,row=0,col=0, img=nil}
--这句是重定义元表的索引，必须要有，
Gem.__index = Gem
--模拟构造体，一般名称为new()
function Gem:new(key, tpe, r, c, mode)
    local self = {}   
    setmetatable(self, Gem)   --必须要有
    self.key = key   
    self.tpe = tpe
    self.row = r
    self.col = c
    
    self.img = nil
    self.anim = nil
    
    self.bomtype = nil
    
    self.animlist = {}
    
    self.isremove = false --已经删除
    self._setpick = true --是否可以拾取
    self.isMoved = false --移动中
    self.isBomed = false --爆炸中
    self.isHeCheng = false --合成中
    self.isSwap = false -- 交换中
    
    -- 删除中
    self.GoToDelete = false
    
    self.action_moveto = nil
    self.action_moveto_anim = nil
    
    local sformat = "TEBrickAnim_0%d.png"
    if mode == PlayMode_TiaoZhan then
        sformat = "CHBrick_0%d.png"
    end
    
    local spn = string.format(sformat, GemTypes[self.tpe]+1)
    --print(spn)
    self.img = CCSprite:createWithSpriteFrameName(spn)
    self.img:setAnchorPoint(ccp(0.5, 0.5))
    self.img:setPosition(ccp(getGemPos(r, c)))
    _GemTypesbatchNode:addChild(self.img, 0, key)
    
    return self  
end

function Gem:onAnimtion()
    --动画动作
    local animation = CCAnimation:create()
    local name = nil
    for i = 0, 3 do
        local sformat = "TEBrickAnim_0%d.png"
        local inde = GemTypes[self.tpe]+1 + 7*i
        if string.len(string.format("%d", inde)) > 1 then
            sformat = "TEBrickAnim_%d.png"
        end
        
        local spn = string.format(sformat, inde)
        --print(spn)
        local pFrame = CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(spn);
        animation:addSpriteFrame(pFrame)
    end
    animation:setDelayPerUnit(0.15)
    animation:setRestoreOriginalFrame(true)
    animation:setLoops(2)--循环次数
    local array = CCArray:create()
    array:addObject(CCAnimate:create(animation))
    
    self.img:runAction(CCSequence:create(array))
end

--是否可对其操作/消除/移动
function Gem:onIsPuz()
    if self.isremove or self.isMoved or self.isBomed or self.GoToDelete then
        return false
    end
    
    return true
end
function Gem:onIsPuzGoBomss()
    if self.isremove or self.isBomed or self.GoToDelete or self.isHeCheng or self.isSwap then
        return true
    end
    
    return false
end

-- 拾取判断
function Gem:isPick(x, y)
    local ispick = false
    local pos_x, pos_y = self.img:getPosition()
    local left = pos_x - gemSize/2
    local right = pos_x + gemSize/2
    local up = pos_y + gemSize/2
    local down = pos_y - gemSize/2
    if (x >= left and x <= right and y >= down and y <= up) then
        ispick = true
    end
    return ispick
end
-- 移动宝石
function Gem:onMoveTo(x, y)
    self.img:setPosition(ccp(x,y))
    if self.anim then
        self.anim:setPosition(ccp(x,y))
    end
    
end
function Gem:GetPosition()
    return self.img:getPosition()
end
function Gem:GetPositionDir()
    return getGemPos(self.row, self.col)
end

-- 隐藏
function Gem:setVisible(bhide)
    self.img:setVisible(bhide)
    if self.amin then
        self.amin:setVisible(bhide)
    end
end

--删除宝石
function Gem:Remove(layer, _GemTypesbatchNode)
    self.isremove = true
    if self.img then
        _GemTypesbatchNode:removeChild(self.img, true)
    end
    if self.anim then
        layer:removeChild(self.anim, true)
    end
    self:stopActions_Moveto()
    self.img = nil
    self.anim = nil
    
    if _GGemTableKeys[self.key] ~= nil then
        _GGemTableKeys[self.key] = nil
    end
    
    _Swap_Gems_EndStop(nil, self.key, true)
end

-- 恢复状态
function Gem:rState()
    self:onPick(false)
end
-- 拾取状态
function Gem:onPick(ispick)
    if ispick == nil then
        ispick = true
    end
    
    --if ispick then
    --    self.img:setColor(ccc3(255, 0, 0))
    --else
    --    self.img:setColor(ccc3(255, 255, 255))
    --end
end

function Gem:setPick(bpick)
    if bpick ~= nil then
        self._setpick = bpick
    end
    
    if not self:onIsPuz() then
        return false
    end
    
    return self._setpick
end

--移动停止
function Gem:onMoveEnd()
    self:stopActions_Moveto()
    
    local act1 = CCScaleTo:create(0.1, 0.7)
    local act2 = CCScaleTo:create(0.1, 1.0)
    local act3 = CCScaleTo:create(0.3, 0.85)
    local act4 = CCScaleTo:create(0.3, 1.0)
    
    local array = CCArray:create()
    array:addObject(act1)
    array:addObject(act2)
    --array:addObject(act3)
    --array:addObject(act4)
    local seq    = CCSequence:create(array)
    self.img:runAction(seq)
    
    Play_Effect(sme_BrickDown)
end
		
function Gem:runAction(action, action2)
    if self.isremove then
        return
    end
    
    if self.img ~= nil then
        self.img:runAction(action)
    end
end

function Gem:stopActions_Moveto()
    _func_DelMoveToGems(self)
    if self.img ~= nil then
        _Swap_Gems_EndStop(nil, self.key)
    end
    self.isMoved = false
    
    if self.img ~= nil and self.action_moveto ~= nil then
        self.img:stopAction(self.action_moveto)
        self.action_moveto = nil
    end
    if self.anim ~= nil and self.action_moveto_anim ~= nil then
        self.anim:setPosition(getGemPos(self.row, self.col))
        --print("+_+1")
        self.anim:stopAction(self.action_moveto_anim)
        self.action_moveto_anim = nil
    end
end

function Gem:runAction_MoveTo(tt, pos, func)
    if func ~= nil then
        self:stopActions_Moveto()
    end
        
    self.action_moveto = CCMoveTo:create(gdvc(tt), pos)
    if func ~= nil then
        self.action_moveto = CCSequence:createWithTwoActions(self.action_moveto, CCCallFuncN:create(func))
    
        self.isMoved = true
    end
    self.img:runAction(self.action_moveto)
    
    if self.anim then
        self.action_moveto_anim = CCMoveTo:create(gdvc(tt), pos)
        self.anim:runAction(self.action_moveto_anim)
    end
end

function Gem:runAction_FadeOut(tt, func)
    local action = CCFadeTo:create(tt, 0)
    if func ~= nil then
        action = CCSequence:createWithTwoActions(action, CCCallFuncN:create(func))
    end
    if self.img then
        self.img:runAction(action)
    end
    
    if self.anim then
        self.anim:runAction(CCFadeTo:create(tt, 0))
    end
end

-- 交换位置
function Gem:swapTo(r, c, func)
    local x,y = getGemPos(r, c)
    -- 缩小放大
    self.img:setScale(0.8)
    self.img:runAction(CCScaleTo:create(Anim_SwapTimes, 1.0))
    
    self:runAction_MoveTo(gdv(Anim_SwapTimes), ccp(x,y), func)
    --print("swapTo",self.key)
    
    self.isSwap = true
end

-- 消除/爆炸
function Gem:on_Puz()
    --Play_Effect(sme_BrickFadeOut)
end

-- 是否是炸弹
function Gem:isBom()
    return self.bomtype
end

-- 变成炸弹
function Gem:changeToBom(bom, layer)
    if self.anim then
        layer:removeChild(self.anim, true)
        self.anim = nil
    end
    
    self.animlist = {}
    self.bomtype = bom
    local action = nil
    local fturext = ""
    local iframe = 0
    local fps = 0.1
    --添加炸弹效果 bom == Item_Bom
    if bom == Item_Bom then
        iframe = 7
        fps = 0.05
        fturext = "bomb001_0000"
    elseif bom == Item_BomArce then
        iframe = 4
        fturext = "aoebomb_0000"
    elseif bom == Item_BomCorss then
        iframe = 2
        fturext = "thunderbomb_0000"
    elseif bom == Item_BomRandom then
        --如果是随机爆炸则样式变成随机炸弹
        iframe = 6
        fturext = "bom_random_00"
        -- 隐藏砖块
        self.img:setVisible(false)
        self.tpe = 0x10
    end
    
    if fturext ~= "" then
        local mx,my = getGemPos(self.row, self.col)
        self.anim = GFunc_CreateAnimation(fturext, 1, iframe, 10000, fps)
        self.anim:setPosition(mx, my)
    end
    
    layer:addChild(self.anim,3)
    
    return self.anim
end





